package com.booktrade.service.impl;

import com.booktrade.entity.Orders;
import com.booktrade.entity.Reviews;
import com.booktrade.entity.Users;
import com.booktrade.mapper.ReviewsMapper;
import com.booktrade.service.IOrdersService;
import com.booktrade.service.IReviewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booktrade.service.IUsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 评价表，存储用户对交易的评价信息 服务实现类
 * </p>
 *
 */
@Service
@RequiredArgsConstructor
public class ReviewsServiceImpl extends ServiceImpl<ReviewsMapper, Reviews> implements IReviewsService {
    private final IUsersService usersService;
    private final IOrdersService ordersService;
    @Override
    @Transactional
    public void addReviews(Reviews reviews, Integer reviewerId) {
        reviews.setReviewerId(reviewerId);
        switch (reviews.getRating()){
            case "好评":
                reviews.setScore(1);
                break;
            case "中评":
                reviews.setScore(0);
                break;
            case "差评":
                reviews.setScore(-1);
                break;
            default:
                reviews.setRating("好评");
                reviews.setScore(1);;
        }
        this.save(reviews);
        Orders orders = ordersService.getById(reviews.getOrderId());
        orders.setStatus("已完成");
        ordersService.updateById(orders);
        Integer sellerId = orders.getSellerId();
        Users users = usersService.getById(sellerId);
        users.setId(sellerId);
        users.setScore(users.getScore()+reviews.getScore());
        usersService.updateById(users);
    }
}
